package com.quyeying.controller;

import cn.hutool.json.JSONNull;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quyeying.annotation.PassToken;
import com.quyeying.utils.PageUtil;
import com.quyeying.utils.result.R;
import com.quyeying.utils.result.ResultEnum;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import com.quyeying.service.SysPlatformConfigService;
import com.quyeying.entity.SysPlatformConfigEntity;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* <p>
* sys_platform_config 前端控制器
* </p>
* @author skx
* @since 2022-07-18
*/
@Slf4j
@RestController
@RequestMapping("platformConfigEntity")
public class SysPlatformConfigController {

    @Resource
    private SysPlatformConfigService sysPlatformConfigService;


    /**
     * 获取平台配置数据接口
     */
    @GetMapping("/findInfo")
    public R findInfo(){
        try {
            SysPlatformConfigEntity platformConfig = sysPlatformConfigService.getOne(Wrappers.query());
            return R.success(platformConfig);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 新增or编辑平台设置
     */
    @PostMapping("/addOrUpdate")
    public R addOrUpdate(@RequestBody SysPlatformConfigEntity sysPlatformConfigEntity){
        try {
            sysPlatformConfigService.saveOrUpdate(sysPlatformConfigEntity);
            return R.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * 新增
    **/
    @PostMapping("/add")
    public R add(@RequestBody SysPlatformConfigEntity sysPlatformConfigEntity){
        try {
            return sysPlatformConfigService.add(sysPlatformConfigEntity);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * 删除
    **/
    @DeleteMapping("/del")
    public R delete(Long id){
        try {
            return sysPlatformConfigService.delete(id);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * 更新
    **/
    @PostMapping("/update")
    public R update(@RequestBody SysPlatformConfigEntity sysPlatformConfigEntity){
        try {
            return sysPlatformConfigService.updateData(sysPlatformConfigEntity);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * 分页查询
    **/
    @GetMapping("/findListByPage")
    public R findListByPage(@RequestParam Integer current,
    @RequestParam Integer size){
        try {
            return sysPlatformConfigService.findListByPage(current, size);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * id查询
    **/
    @GetMapping("/findById")
    public R findById(Long id){
        try {
            return sysPlatformConfigService.findById(id);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
     * id查询
     **/
    @GetMapping("/findSysEdition")
    @PassToken
    public R findSysEdition(){
        try {
            Map<String, Object> map = new HashMap<>(1);
            map.put("sysEdition",sysPlatformConfigService.getOne(Wrappers.query()).getAppVersion());
            return R.success(map);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }
}
